package com.hanbang.chu_xing_gao_zhao.view_me;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.hanbang.chu_xing_gao_zhao.application.HttpInterfaces;
import com.hanbang.chu_xing_gao_zhao.base.BaseActivity;
import com.hanbang.chu_xing_gao_zhao.entity.CommentData;
import com.hanbang.chu_xing_gao_zhao.utils.JsonHelper;
import com.hanbang.chu_xing_gao_zhao.utils.MyRequestCallBack;
import com.hanbang.chu_xing_gao_zhao.utils.MySharedPreferences;
import com.hanbang.chu_xing_gao_zhao.utils.MyToast;
import com.hanbang.chu_xing_gao_zhao.utils.SystemTools;
import com.hanbang.chu_xing_gao_zhao.utils.UiUtils;
import com.hanbang.cxgz_2.R;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * 我的信息
 */
public class MeInforActivity extends BaseActivity {

    /**
     * 评论列表
     */
    private ArrayList<CommentData> listData = new ArrayList<CommentData>();
    private PullToRefreshListView listView;
    private ListViewAdapter adapter;
    private int pageIndex = 1;
    private int pageSize = 10;
    int id;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.me_infor);

        initView();
        setOnClickListener();
    }

    /**
     * 初始化view
     */
    private void initView() {
        setTitleMore(View.VISIBLE, View.INVISIBLE, "我的消息", null, null, null);
        setBackArrows(findViewById(R.id.bar_top_conver_classify_back));


        Intent intent = getIntent();
//        id = intent.getIntExtra("id", 0);
        id = 105;

        listView = (PullToRefreshListView) findViewById(R.id.lv_me_infor_list);
        //初始化评论列表
        adapter = new ListViewAdapter(listData);
        listView.setAdapter(adapter);

        //调评论接口
        RequestParams paramsB = new RequestParams();
        paramsB.addBodyParameter("telphone", MySharedPreferences.getSharedPreferencesKeyAndValue(this, MySharedPreferences.USER_DATA, "tlephone"));
        paramsB.addBodyParameter("password", MySharedPreferences.getSharedPreferencesKeyAndValue(this, MySharedPreferences.USER_DATA, "password"));
        paramsB.addBodyParameter("id", id + "");
        paramsB.addBodyParameter("type", 4 + "");
        paramsB.addBodyParameter("pageIndex", pageIndex + "");
        paramsB.addBodyParameter("pageSize", pageSize + "");
        getHttpList(HttpInterfaces.Comment_List, paramsB);

    }

    /**
     * 设置点击事件
     */
    private void setOnClickListener() {
        listView.setMode(PullToRefreshBase.Mode.BOTH);

        //上下刷新
        listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                pageIndex = 1;
                listData.clear();

                RequestParams params = new RequestParams();
                params.addBodyParameter("telphone", userData.getTelphone());
                params.addBodyParameter("password", userData.getPassword());
                params.addBodyParameter("id", id + "");
                params.addBodyParameter("type", 4 + "");
                params.addBodyParameter("pageIndex", pageIndex + "");
                params.addBodyParameter("pageSize", pageSize + "");
                getHttpList(HttpInterfaces.Comment_List, params);

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                pageIndex++;

                RequestParams params = new RequestParams();
                params.addBodyParameter("telphone", userData.getTelphone());
                params.addBodyParameter("password", userData.getPassword());
                params.addBodyParameter("id", id + "");
                params.addBodyParameter("type", 4 + "");
                params.addBodyParameter("pageIndex", pageIndex + "");
                params.addBodyParameter("pageSize", pageSize + "");
                getHttpList(HttpInterfaces.Comment_List, params);
            }
        });
    }

    /**
     * 处理监听
     */
    @SuppressWarnings("unused")
    private OnClickListener onClickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {

                default:
                    break;
            }
        }
    };

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

    /**
     * 获取我的订单
     */
    private void getHttpList(String url, RequestParams params) {
        httpUtils.send(HttpRequest.HttpMethod.POST, url, params, new MyRequestCallBack<String>(this, "加载中...") {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                super.onSuccess(responseInfo);
                try {
                    JSONObject json = new JSONObject(responseInfo.result);

                    //获取列表

                    int result = json.getInt("Result");
                    if (result == 0) {
                        JSONArray data = json.getJSONArray("list");

                        if (data.length() == 0) {
                            MyToast.show(MeInforActivity.this, "没有更多内容", 1);
                        }
                        listData.addAll(JsonHelper.parseCollection(data, ArrayList.class, CommentData.class));
                        adapter.notifyDataSetChanged();
                        listView.onRefreshComplete();
                    }


                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }


    /**
     * adapter 设置
     *
     * @author Administrator
     */
    class ListViewAdapter extends BaseAdapter {

        public ArrayList<CommentData> list = new ArrayList<CommentData>();

        public ListViewAdapter(ArrayList<CommentData> listData) {
            this.list = listData;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public CommentData getItem(int position) {

            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            final ViewHolder viewHolder;
            if (convertView == null) {// 是空加载tiem
<<<<<<< HEAD
                convertView = getLayoutInflater().inflate(R.layout.item_me_infor, null);
=======
                convertView = getLayoutInflater().inflate(R.layout.item_me_orde, null);
>>>>>>> b59a9d3935c48dfc1ddd2717b231eb6ba5587469
                UiUtils.applyFont(convertView);


                viewHolder = new ViewHolder();
                // 子控件初始化
<<<<<<< HEAD
                viewHolder.hint = (TextView) convertView.findViewById(R.id.tv_item_me_infor_hint);
                viewHolder.time = (TextView) convertView.findViewById(R.id.tv_item_me_infor_time);
=======
                viewHolder.portraitId = (ImageView) convertView.findViewById(R.id.item_me_orde_image);
                viewHolder.date = (TextView) convertView.findViewById(R.id.item_me_orde_date);
                viewHolder.name = (TextView) convertView.findViewById(R.id.item_me_orde_name);
                viewHolder.pay = (TextView) convertView.findViewById(R.id.item_me_orde_pay);
                viewHolder.affirmOrde = (TextView) convertView.findViewById(R.id.item_me_orde_affirmOrde);
>>>>>>> b59a9d3935c48dfc1ddd2717b231eb6ba5587469


                convertView.setTag(viewHolder);// 把现有有item对象存起来
            } else {// 不是空复用

                viewHolder = (ViewHolder) convertView.getTag();
            }

            final CommentData data = getItem(position);

<<<<<<< HEAD

            viewHolder.hint.setText(data.getText());
            viewHolder.time.setText(data.getUser_name());
=======
            bitmapUtils.display(viewHolder.portraitId, SystemTools.getHttpFileUrl(data.getAvatar()));

            viewHolder.date.setText(data.getText());
            viewHolder.name.setText(data.getUser_name());
            viewHolder.pay.setText(data.getText());
>>>>>>> b59a9d3935c48dfc1ddd2717b231eb6ba5587469


            return convertView;
        }

        class ViewHolder {// 进行缓存保存
            // 子控件主要是里面的数据
<<<<<<< HEAD

            private TextView hint;
            private TextView time;

=======
            private ImageView portraitId;
            private TextView date;
            private TextView name;
            private TextView pay;
            private TextView affirmOrde;
>>>>>>> b59a9d3935c48dfc1ddd2717b231eb6ba5587469

        }

    }

}
